package com.briup.jz.service.impl;

import com.briup.jz.bean.User;
import com.briup.jz.bean.extend.UserExtend;
import com.briup.jz.dao.UserMapper;
import com.briup.jz.dao.extend.UserExtendMapper;
import com.briup.jz.service.IUserService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: jz
 * @Author: xjs
 * @Description: 用户业务实现
 * @Date: 2020/6/12 13:45
 */
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserExtendMapper userExtendMapper;

    /**
     * @param user
     * @return void
     * @Author xjs
     * @Description 添加或更新用户；如果传入id，更新用户信息；否则添加
     * @Date 2020/6/12 13:47
     */
    @Override
    public void saveOrUpdate(User user) throws CustomerException {
        if (user.getId() == null){
            user.setRegisterTime(System.currentTimeMillis());
            user.setLastLoginTime(System.currentTimeMillis());
            userMapper.insert(user);
        }else{
            //检查用户是否存在
            User userBefore  = userMapper.selectByPrimaryKey(user.getId());
            if(userBefore == null){
                //要修改的用户不存在
                throw new CustomerException("要修改的数据不存在");
            }
            //修改用户信息时不修改其状态
//            user.setStatus(userBefore.getStatus());
            userMapper.updateByPrimaryKey(user);
        }
    }

    /**
     * @param id
     * @return void
     * @Author xjs
     * @Description 根据id删除用户
     * @Date 2020/6/12 13:47
     */
    @Override
    public void deleteById(Long id) throws CustomerException {
        // 先判断该id对应的数据存在不？
        User user = userMapper.selectByPrimaryKey(id);
        if(user == null){
            //当不存在，报错！删除
            throw new CustomerException("删除失败，要删除的数据不存在");
        }
        // 当存在，删除
        userMapper.deleteByPrimaryKey(id);
    }

    /**
     * @return java.util.List<com.briup.jz.bean.impl.UserExtend>
     * @Author xjs
     * @Description 查询所有用户以及其角色
     * @Date 2020/6/12 13:47
     */
    @Override
    public List<UserExtend> findAllWithRole() {
        return userExtendMapper.selectAllWithRole();
    }

    /**
     * @param id
     * @return com.briup.jz.bean.extend.UserExtend
     * @Author xjs
     * @Description 根据id查询用户及其角色信息
     * @Date 2020/6/13 16:28
     */
    @Override
    public UserExtend findByIdWithRole(Long id) {
        return userExtendMapper.selectByIdWithRole(id);
    }

    /**
     * @param id
     * @return void
     * @Author xjs
     * @Description 设置用户禁用
     * @Date 2020/6/13 17:11
     */
    @Override
    public void updateStatus(Long id) throws CustomerException {
        //判断数据是否存在
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null){
            //用户不存在
            throw new CustomerException("修改失败，用户不存在");
        }
        //判断用户当前状态
        if (user.getStatus().equals("用户正常")){
            //当前用户正常，修改为禁用
            userExtendMapper.updateStatus(id,UserExtend.USERFORBIDDEN);
        }
//        else if (user.getStatus().equals("未审核")) {
//            throw new CustomerException("用户未审核");
//        }
        else
        {
            //当前用户状态为禁用，修改为正常
            userExtendMapper.updateStatus(id,UserExtend.USERNORMAL);
        }
    }

    /**
     * @param id
     * @param userFace
     * @return void
     * @Author xjs
     * @Description 根据id来修改用户的头像
     * @Date 2020/6/13 17:28
     */
    @Override
    public void updateUserFace(Long id, String userFace) throws CustomerException {
        //判断数据是否存在
        User user = userMapper.selectByPrimaryKey(id);
        if(user == null){
            //数据不存在
            throw new CustomerException("用户不存在，修改失败");
        }
        userExtendMapper.updateUserFace(id, userFace);
    }

    /**
     * @param id
     * @param privilege
     * @return void
     * @Author xjs
     * @Description 查询用户是否有权限（用户id ，权限名称）
     * @Date 2020/6/13 22:43
     */
    @Override
    public void checkPrivilege(Long id, String privilege) throws CustomerException {
        if (userExtendMapper.checkPrivilege(id, privilege) == 0 ){
            throw new CustomerException("当前用户无权限");
        }
    }

    @Override
    public List<UserExtend> findAllEmployee() {
        return userExtendMapper.findAllEmployee();
    }

    @Override
    public List<UserExtend> findAllEmployeeAndStatus() {
        return userExtendMapper.findAllEmployeeAndStatus();
    }

    @Override
    public List<UserExtend> findUserByName(String name) {
        return userExtendMapper.selectUserByName(name);
    }
}
